<?php

	// +----------------------------------------------------------------------
	// | 安全配置命令文件
	// +----------------------------------------------------------------------
	// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
	// +----------------------------------------------------------------------
	// | Author: 微尘 <yicmf@qq.com>
	// +----------------------------------------------------------------------

	namespace app\common\command;

	use app\common\event\Setting as SettingEvent;
	use think\console\Command;
	use think\console\Input;
	use think\console\input\Argument;
	use think\console\input\Option;
	use think\console\Output;
	use think\facade\Config;

	class Safe extends Command
	{
		protected function configure()
		{
			$this->setName('safe')
				->addArgument('name', Argument::OPTIONAL, "参数名称")
				->addOption('value', null, Option::VALUE_REQUIRED, '配置值')
				->setDescription('安全配置');
		}

		protected function execute(Input $input, Output $output)
		{
			if ('cli' !== PHP_SAPI) {
				$output->writeln('请使用命令行获取');
			} else {
				$name = trim($input->getArgument('name'));
				if ($input->hasOption('value')) {
					// 配置新值
					$value = trim($input->getOption('value'));
					$safe = Config::get('safe');
					if ('url' == $name) {
						$safe['url'] = $value;
						SettingEvent::update($safe, 'safe');
						$output->writeln('当前配置安全登录入口更新为：http(s)://' . Config::get(Config::get('yicmf.project') . '.host') . '/' . $value . '.html');
					} elseif ('admin' == $name) {
						$output->writeln('管理员密码变更为' . $value);
					} else {
						$safe['open'] = $value ? 1 : 0;
						SettingEvent::update($safe, 'safe');
						$output->writeln('当前安全登录入口状态【' . ($value ? '打开' : '关闭') . '】');
					}
				} else {
					// 读取
					if ('url' == $name) {
						$output->writeln('当前配置安全登录入口是：http(s)://' . Config::get(Config::get('yicmf.project') . '.host') . '/' . Config::get('safe.url') . '.html');
					} else {
						$output->writeln('当前安全登录入口状态【' . (Config::get('safe.open') ? '打开' : '关闭') . '】');
					}
				}
			}
		}


	}